package br.com.android.games.daedal.flat;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;

public class Edge {

	private int selected = 0;
	private int[] l = new int[16];
	private int[] s = new int[16];
	private int[] o;
	private int scale;
	private int offset;
	private int color;
	private Drawable selc[];

	/**
	 * Back groud color
	 */
	private Paint mStickPaint;

	public Edge(int[] local, int offset, int scale, float stroke,
			Drawable selc[]) {

		int w = (int) stroke * 2 + 1;
		int wi = (scale + offset) / 5;
		this.scale = scale;
		this.offset = offset;
		this.o = local;
		this.selc = selc;
		if (local[1] == local[3]) {
			l[8] = l[0] = (local[0] * scale + offset);
			l[9] = l[1] = (local[1] * scale + offset) + w;
			l[12] = l[2] = (local[0] * scale + offset);
			l[13] = l[3] = (local[1] * scale + offset) - w;
			l[10] = l[4] = (local[2] * scale + offset);
			l[11] = l[5] = (local[3] * scale + offset) + w;
			l[14] = l[6] = (local[2] * scale + offset);
			l[15] = l[7] = (local[3] * scale + offset) - w;
			//
			s[8] = s[0] = (local[0] * scale + offset);
			s[9] = s[1] = (local[1] * scale + offset) + wi;
			s[12] = s[2] = (local[0] * scale + offset);
			s[13] = s[3] = (local[1] * scale + offset) - wi;
			s[10] = s[4] = (local[2] * scale + offset);
			s[11] = s[5] = (local[3] * scale + offset) + wi;
			s[14] = s[6] = (local[2] * scale + offset);
			s[15] = s[7] = (local[3] * scale + offset) - wi;
		} else {
			l[8] = l[0] = (local[0] * scale + offset) + w;
			l[9] = l[1] = (local[1] * scale + offset);
			l[12] = l[2] = (local[0] * scale + offset) - w;
			l[13] = l[3] = (local[1] * scale + offset);
			l[10] = l[4] = (local[2] * scale + offset) + w;
			l[11] = l[5] = (local[3] * scale + offset);
			l[14] = l[6] = (local[2] * scale + offset) - w;
			l[15] = l[7] = (local[3] * scale + offset);
			//
			s[8] = s[0] = (local[0] * scale + offset) + wi;
			s[9] = s[1] = (local[1] * scale + offset);
			s[12] = s[2] = (local[0] * scale + offset) - wi;
			s[13] = s[3] = (local[1] * scale + offset);
			s[10] = s[4] = (local[2] * scale + offset) + wi;
			s[11] = s[5] = (local[3] * scale + offset);
			s[14] = s[6] = (local[2] * scale + offset) - wi;
			s[15] = s[7] = (local[3] * scale + offset);
		}

		mStickPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mStickPaint.setStrokeWidth(stroke);
		mStickPaint.setStyle(Style.FILL);
	}

	public boolean isInside(int x, int y) {

		boolean result = false;
		int sx = Integer.MAX_VALUE, lx = -1;

		for (int i = 0; i < 16; i += 2) {
			if (sx > s[i])
				sx = s[i];
			if (lx < s[i])
				lx = s[i];
		}

		if ((x >= (sx)) && (x <= (lx))) {
			sx = Integer.MAX_VALUE;
			lx = -1;
			for (int i = 1; i < 16; i += 2) {
				if (sx > s[i])
					sx = s[i];
				if (lx < s[i])
					lx = s[i];
			}
			result = (y >= (sx)) && (y <= (lx));
		}

		return result;
	}

	public void setSelected(int selected, int color) {
		this.selected = selected;
		this.color = color;
	}
	
	public int getSelected(){
		return this.selected;
	}

	public void paint(Canvas canvas) {
		if (selected == 1) {
			mStickPaint.setColor(color);
			for (int i = 0; i < 16; i += 4) {
				canvas.drawLine(l[0 + i], l[1 + i], l[2 + i], l[3 + i],
						mStickPaint);
			}
		}

		if (selected >= 2) {
			int imgl = (offset / 3);
			int idx  = (selected - 2) * 2;
			selc[0].setBounds(o[idx + 0] * scale + offset - imgl, o[idx + 1]
					* scale + offset - imgl,
					o[idx + 0] * scale + offset + imgl, o[idx + 1] * scale
							+ offset + imgl);
			selc[0].draw(canvas);

			if (o[0] == o[2]) {
				if (selected == 2) {
					selc[1].setBounds(o[idx + 0] * scale + offset - scale / 2,
						              o[idx + 1] * scale + offset, 
						              o[idx + 0] * scale + offset - scale / 2 + scale, 
						              o[idx + 1] * scale + offset + scale);
					selc[1].draw(canvas);
				} else {
					selc[2].setBounds(o[0] * scale + offset - scale / 2,
							          o[1] * scale + offset, 
							          o[0] * scale + offset - scale / 2 + scale, 
							          o[1] * scale + offset + scale);
					selc[2].draw(canvas);
				}
			} else {
				if (selected == 2) {
					selc[3].setBounds(o[idx + 0] * scale + offset,
							          o[idx + 1] * scale + offset - scale / 2, 
							          o[idx + 0] * scale + offset + scale, 
							          o[idx + 1] * scale + offset - scale / 2 + scale);
					selc[3].draw(canvas);
				} else {
					selc[4].setBounds(o[0] * scale + offset,
							          o[1] * scale + offset - scale / 2, 
							          o[0] * scale + offset + scale, 
							          o[1] * scale + offset- scale / 2 + scale);
					selc[4].draw(canvas);
				}
			}
		}
	}

	public int isOver(int selectedX, int selectedY, double r) {
		int result = 0;

		int xc = o[0] * scale + offset;
		int yc = o[1] * scale + offset;

		double dc = ((xc - selectedX) * (xc - selectedX)
				    + (yc - selectedY) * (yc - selectedY));

		if (dc < (r*r))
			result = 1;
		else {
			xc = o[2] * scale + offset;
			yc = o[3] * scale + offset;

			dc = ((xc - selectedX) * (xc - selectedX)
				+ (yc - selectedY) * (yc - selectedY));

			if (dc < (r*r))
				result = 2;
		}

		return result;
	}
	
	public boolean isLyingOn(int x, int y){
		boolean result = false;
		
		if (getSelected() > 1){
			int oidx = (selected - 2) * 2;
			int didx = ((selected - 1) % 2) * 2;			
			int xo = o[oidx+0] * scale + offset;
			int yo = o[oidx+1] * scale + offset;
			int xd = o[didx+0] * scale + offset;
			int yd = o[didx+1] * scale + offset;
			int vx = xd-xo;
			int vy = yd-yo;
			int vs = x -xo;
			int vw = y -yo;		
			double len=Math.sqrt(vs*vs+vw*vw);
			
			if (len > (scale / 2)){
				double ang=Math.acos((vx*vs+vy*vw) / (scale * len));
				result = (Math.PI/6) > ang;		
			}
		}		
		
		return result;
	}
}
